/**
 * Created by Administrator on 2017/10/13.
 */
let app = new Vue({
    el: "#banner",
    data: {
        resources: {
            bannerImgList:[],
            tempPath:"",
            submitType:"",
            currentBanner:"",
            imgPath: fileServerPath,
        }
    },
    mounted: function () {
        this.getBannerImgList()
    },
    methods: {
        getBannerImgList:function () {
            $.get(rootPath+"/banner", res => {
                if(res.meta.code === 200){
                    this.resources.bannerImgList = res.data;
                }
            })
        },
        /**
         * 上传banner图
         */
        addBannerImg:function () {
            let files = document.querySelector("#pic_banner").files;
            //上传数据
            uploadFile(files).then((res) => {
                if (res.meta.code === 200) {
                    this.resources.tempPath = res.data[0].savePath;
                } else alert("图片上传失败--" + res.meta.code)
            });
        },
        /**
         * 保存banner信息
         */
        sendInfo:function () {
            let nv = new NiceValidate([
                {dom: document.getElementById("banner_desc")},
                {dom: document.getElementById("sort_order")},
            ]);
            let data = nv.niceValidate();
            if(data){
                if(this.resources.tempPath){
                    data.img_url = this.resources.tempPath;
                    $.post(rootPath+"/banner"+this.resources.submitType,data, (res)=> {
                        this.getBannerImgList()
                        this.resources.currentBanner = "";
                        this.resources.submitType = "";
                        this.resources.tempPath = ""
                    })
                }else {
                    alert("请选择图片")
                }
            }else {
                alert("请完善您的信息")
            }
        },
        /**
         * 编辑
         * @param e
         */
        preEdit:function (e) {
            let bannerId = e.target.getAttribute("banner-id");
            for(let item in this.resources.bannerImgList){
                if(this.resources.bannerImgList[item].id == bannerId){
                    this.resources.currentBanner = this.resources.bannerImgList[item];
                    this.resources.submitType = "/"+bannerId;
                    this.resources.tempPath = this.resources.bannerImgList[item].img_url
                }
            }
        },
        /**
         * 删除单个banner
         * @param e
         */
        deleteBanner:function (e) {
            let bannerId = e.target.getAttribute("banner-id");
            $.get(rootPath+"/banner/delete/"+bannerId, (res)=> {
                if(res.meta.code === 200){
                    alert("删除成功")
                    this.getBannerImgList()
                }else {
                    alert("删除失败--"+res.meta.message)
                }
            })
        }
    }
})